package com.course.servlet;

import com.course.entity.Course;
import com.course.entity.Teacher;
import com.course.service.CourseService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/laoshi_edit_course")
public class LaoshiEditCourseServlet extends HttpServlet {
    private final CourseService courseService = new CourseService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        HttpSession session = request.getSession(false);
        Teacher teacher = null;
        if (session != null) {
            teacher = (Teacher) session.getAttribute("teacher");
        }

        if (teacher == null) {
            response.sendRedirect(request.getContextPath() + "/laoshi_login.jsp");
            return;
        }

        String idStr = request.getParameter("id");
        if (idStr != null && !idStr.isEmpty()) {
            try {
                int courseId = Integer.parseInt(idStr);
                Course course = courseService.getCourseById(courseId);

                if (course != null && course.getTeacherId() == teacher.getId()) {
                    request.setAttribute("course", course);
                    request.getRequestDispatcher("/laoshi_edit_course.jsp").forward(request, response);
                } else {
                    request.setAttribute("errorMessage", "课程不存在或您无权编辑此课程。");
                    request.getRequestDispatcher("/laoshi_my_courses.jsp").forward(request, response);
                }
            } catch (NumberFormatException e) {
                request.setAttribute("errorMessage", "无效的课程ID。");
                request.getRequestDispatcher("/laoshi_my_courses.jsp").forward(request, response);
                e.printStackTrace();
            }
        } else {
            request.setAttribute("errorMessage", "课程ID不能为空。");
            request.getRequestDispatcher("/laoshi_my_courses.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        HttpSession session = request.getSession(false);
        Teacher teacher = null;
        if (session != null) {
            teacher = (Teacher) session.getAttribute("teacher");
        }

        if (teacher == null) {
            response.sendRedirect(request.getContextPath() + "/laoshi_login.jsp");
            return;
        }

        String idStr = request.getParameter("id");
        String courseName = request.getParameter("courseName");
        String courseDescription = request.getParameter("courseDescription");
        String maxCapacityStr = request.getParameter("maxCapacity");

        if (idStr != null && !idStr.isEmpty() && courseName != null && !courseName.isEmpty() &&
            courseDescription != null && !courseDescription.isEmpty() && maxCapacityStr != null && !maxCapacityStr.isEmpty()) {
            try {
                int id = Integer.parseInt(idStr);
                int maxCapacity = Integer.parseInt(maxCapacityStr);

                Course course = courseService.getCourseById(id);

                if (course != null && course.getTeacherId() == teacher.getId()) {
                    course.setCourseName(courseName);
                    course.setCourseDescription(courseDescription);
                    course.setMaxCapacity(maxCapacity);

                    boolean success = courseService.updateCourse(course);

                    if (success) {
                        request.setAttribute("successMessage", "课程信息更新成功！");
                    } else {
                        request.setAttribute("errorMessage", "课程信息更新失败，请稍后重试。");
                    }
                    request.setAttribute("course", course); // Pass updated course back to JSP
                } else {
                    request.setAttribute("errorMessage", "课程不存在或您无权编辑此课程。");
                }
            } catch (NumberFormatException e) {
                request.setAttribute("errorMessage", "无效的输入格式，请检查课程ID或最大容量。");
                e.printStackTrace();
            }
        } else {
            request.setAttribute("errorMessage", "所有字段都不能为空。");
        }
        request.getRequestDispatcher("/laoshi_edit_course.jsp").forward(request, response);
    }
} 